<template>
  <view class="webview-container">
    <web-view
      :src="webviewUrl"
      @message="handleMessage"
      @error="handleError"
    ></web-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      webviewUrl: "",
      pageTitle: "文档预览",
    };
  },

  onLoad(options) {
    if (options.url) {
      this.webviewUrl = decodeURIComponent(options.url);
    }
    if (options.title) {
      this.pageTitle = decodeURIComponent(options.title);
      uni.setNavigationBarTitle({
        title: this.pageTitle,
      });
    }
  },

  methods: {
    handleMessage(event) {
      console.log("WebView消息:", event);
    },

    handleError(event) {
      console.error("WebView错误:", event);
      uni.showToast({
        title: "加载失败",
        icon: "none",
      });
    },
  },
};
</script>

<style lang="scss">
.webview-container {
  width: 100%;
  height: 100vh;
}
</style>
